
global name "A80_cash_allmonths_prep"
cap log close
log using "${pfile}/${name}.smcl", replace smcl


*Parameteres: YEAR   KINK  SLOPE BEN K-N MI MF   YF DF INF \ YEAR   KINK   SLOPE  BEN   K-N MI MF   YF  DF INF \ YEAR   KINK   SLOPE   BEN   K-N MI MF  YF  DF INF \
local y_2001 "2001, 225.00, 0.8, 180, 1, 4, 3, 2002, 31, 2.021 \ 2001, 297.14, -0.3, 235.31, 2, 4, 3, 2002, 31, 2.021 \ 2001, 495.28, -0.5, 335.88, 3, 4, 3, 2002, 31, 2.021"
local y_2002 "2002, 250.00, 0.8, 200, 1, 4, 3, 2003, 31, 1.877 \ 2002, 330.14, -0.3, 264.11, 2, 4, 3, 2003, 31, 1.877 \ 2002, 550.31, -0.5, 374.19, 3, 4, 3, 2003, 31, 1.877" 
local y_2003 "2003, 300.00, 0.8, 240, 1, 4, 3, 2004, 31, 1.668 \ 2003, 396.18, -0.3, 316.94, 2, 4, 3, 2004, 31, 1.668 \ 2003, 660.37, -0.5, 449.03, 3, 4, 3, 2004, 31, 1.668"
local y_2004 "2004, 325.00, 0.8, 260, 1, 4, 4, 2005, 31, 1.526 \ 2004, 429.20, -0.3, 343.36, 2, 4, 4, 2005, 31, 1.526 \ 2004, 715.40, -0.5, 486.46, 3, 4, 4, 2005, 31, 1.526"
local y_2005 "2005, 375.00, 0.8, 300, 1, 5, 3, 2006, 31, 1.419 \ 2005, 495.23, -0.3, 396.18, 2, 5, 3, 2006, 31, 1.419 \ 2005, 825.46, -0.5, 561.30, 3, 5, 3, 2006, 31, 1.419"
local y_2006 "2006, 437.50, 0.8, 350, 1, 4, 3, 2007, 31, 1.342 \ 2006, 577.77, -0.3, 462.22, 2, 4, 3, 2007, 31, 1.342 \ 2006, 963.04, -0.5, 654.85, 3, 4, 3, 2007, 31, 1.342"
local y_2007 "2007, 475.00, 0.8, 380, 1, 4, 2, 2008, 28, 1.302 \ 2007, 627.29, -0.3, 501.83, 2, 4, 2, 2008, 28, 1.302\ 2007, 1045.48, -0.5, 710.93, 3, 4, 2, 2008, 28, 1.302"
local y_2008 "2008, 518.75, 0.8, 415, 1, 3, 1, 2009, 31, 1.246 \ 2008, 685.06, -0.3, 548.05, 2, 3, 1, 2009, 31, 1.246 \ 2008, 1141.88, -0.5, 776.46, 3, 3, 1, 2009, 31, 1.246"
local y_2009 "2009, 581.25, 0.8, 465, 1, 2, 12, 2009, 31, 1.177 \2009, 767.60, -0.3, 614.08, 2, 2, 12, 2009, 31, 1.177 \ 2009, 1279.46, -0.5, 870.01, 3, 2, 12, 2009, 31, 1.177"
local y_2010 "2010, 637.50, 0.8, 510, 1, 1, 12, 2010, 31, 1.128 \ 2010, 841.88, -0.3, 673.50, 2, 1, 12, 2010, 31, 1.128 \ 2010, 1403.28, -0.5, 954.20, 3, 1, 12, 2010, 31, 1.128"
local y_2011 "2011, 675.00, 0.8, 540, 1, 1, 12, 2011, 31, 1.065 \ 2011, 891.40, -0.3, 713.12, 2, 1, 12, 2011, 31, 1.065 \ 2011, 1485.83, -0.5, 1010.35, 3, 1, 12, 2011, 31, 1.065"
local y_2012 "2012, 777.50, 0.8, 622, 1, 1, 12, 2012, 31, 1    \ 2012, 1026.77, -0.3, 821.41, 2, 1, 12, 2012, 31, 1    \ 2012, 1711.45, -0.5, 1163.75, 3, 1, 12, 2012, 31, 1   "
local y_2013 "2013, 847.50, 0.8, 678, 1, 1, 12, 2013, 31, 0.944  \ 2013, 1090.43, -0.3, 872.34, 2, 1, 12, 2013, 31, 0.944  \ 2013, 1817.56, -0.5, 1235.91, 3, 1, 12, 2013, 31, 0.944"
local y_2014 "2014, 905.00, 0.8, 724, 1, 1, 12, 2014, 31, 0.887  \ 2014, 1151.06, -0.3, 920.84, 2, 1, 12, 2014, 31, 0.887    \ 2014, 1918.62, -0.5, 1304.63, 3, 1, 12, 2014, 31, 0.887"
local y_2015 "2015, 985.00, 0.8, 788, 1, 1, 12, 2015, 31, 0.801  \ 2015, 1222.77, -0.3, 978.26, 2, 1, 12, 2015, 31, 0.801    \ 2015, 2038.15, -0.5, 1385.90, 3, 1, 12, 2015, 31, 0.801"
local y_2016 "2016, 1100, 0.8, 880, 1, 1, 12, 2016, 31, 0.753  \ 2016, 1360.70, -0.3, 1088.56, 2, 1, 12, 2016, 31, 0.753    \ 2016, 2268.05, -0.5, 1542.24, 3, 1, 12, 2016, 31, 0.753"

mat sch_y = ( `y_2001' \ `y_2002' \ `y_2003' \ `y_2004' \ `y_2005' \ `y_2006' \ `y_2007' \ `y_2008' \ `y_2009' \ `y_2010' \ `y_2011' \ `y_2012' \ `y_2013' \ `y_2014' \ `y_2015' \ `y_2016' )
matlist sch_y




************ // Defining main sample // *************

use A0_cash_base, clear

keep if year >= 2011 & year <= 2014

keep if abs(x)<=0.1 // 0.8
keep if wkd >= 31  // Abono eligibility (1)
keep if year >= yf_min+5  // Abono eligibility (3)


drop if openjob==1

// setting tenure in months
replace tempempr = tempempr*7/30

// Range - keep all dismissal months for robustness
*drop if dt_dism < mdy(6,1,year)
*drop if dt_dism > mdy(7,15,year)

keep if tpv == 10
drop if natjuridica <= 2038
keep if hor >= 30
drop if ind == 1

g k = x <=0
g kx = k*x
g x2 = x^2
g kx2 = k*x2


* Generating Bonus Value Variable
g bon_inf = .

forvalues i = 6/15 {
local year = sch_y[1+3*(`i'-1),1]
replace bon_inf = sch_y[1+3*(`i'-1),4] * sch_y[1+3*(`i'-1),10] if year == `year'
}

save A8_cash_allmonths, replace



* Adding data on actual eligibility
use A8_cash_allmonths, clear

rename id id_current
rename p_id id
replace year = year-1

gsort year id
merge m:1 year id using "${prais}/2011/1_2011s", keep(1 3) nogen keepus(nome)
rename nome nome11
merge m:1 year id using "${prais}/2012/1_2012s", keep(1 3) nogen keepus(nome)
rename nome nome12
merge m:1 year id using "${prais}/2013/1_2013s", keep(1 3) nogen keepus(nome)
rename nome nome13

g nome = ""
replace nome = nome11 if year==2011
replace nome = nome12 if year==2012
replace nome = nome13 if year==2013
drop nome11 nome12 nome12

cap drop cpf32
g double cpf32 = int(cpf/100)
replace cpf32 = cpf32 - int(cpf32/10^6)*10^6
format cpf cpf32 %13.0g

merge m:1 cpf32 nome using "${pabono}/cef2012", gen(m12) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/cef2013", gen(m13) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/cef2014", gen(m14) keepus(rais) keep(1 3)

merge m:1 cpf32 nome using "${pabono}/bb2012", gen(m12bb) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/bb2013", gen(m13bb) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/bb2014", gen(m14bb) keepus(rais) keep(1 3)

drop rais cpf32 nome

g c12 = m12==3|m12bb==3
g c13 = m13==3|m13bb==3
g c14 = m14==3|m14bb==3

replace year = year+1
rename id p_id
rename id_current id

g cash = (c12&year==2012)|(c13&year==2013)|(c14&year==2014)
replace cash = . if year <=2011

drop m12 m13 m14 m12bb m13bb m14bb

save A8_cash_allmonths, replace

// call script preparing additional variables
do "${dbox}/Brazil_Cash_on_new/Do_restat/A80_cash_allmonths_prepadditionalvars.do"


cap log close




// *** // SAMPLE FOR ADDITIONAL CASH GRANT EFFECTS - WORKERS DISMISSED IN OTHER MONTHS (JAN-MAY + JULY 16TH-DEC) // *** //
use A8_cash_allmonths, clear

drop if x==0

// Preparing vars
*replace tempempr = tempempr * 30/7

merge 1:1 year id using A8_additional, keep(1 3) nogen
replace n_und = (mdy(12,31,2017) - dt_dism)/7 if n_und==.
replace und = n_und
drop n_und

replace mon12 = 0 if mon12==.
replace mon24 = 0 if mon24==.
replace mon36 = 0 if mon36==.
replace inc12 = 0 if inc12==.
replace inc24 = 0 if inc24==.
replace inc36 = 0 if inc36==.

g emp12 = mon12>0 & mon12!=.
g emp24 = mon24>0 & mon24!=.
g emp36 = mon36>0 & mon36!=.

g totmon = mon12+mon24+mon36
g totinc = inc12+inc24+inc36

g totwage = totinc/totmon
g wage12 = inc12/mon12
g wage24 = inc24/mon24
g wage36 = inc36/mon36


foreach x in 3 6 36 { // everything monthly
g sur`x' = und > 30/7 * (`x')
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

forval x = 1/41 { // survival relative to March of displacement year
g sura`x' = dt_dism + und*7 > mdy(6,1,year)+30*(`x'-4) if month(dt_dism)<=12 & dt_dism <= mdy(6,1,year)+30*(`x'-4) & dt_dism+und*7>=mdy(6,1,year)
}

global var "sur* sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage" 


// RD variables
cap drop k kx
g k = (x<=0 & x!=.)
g kx = k * x

save A_cash_othermonths, replace


